from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from info import create_app,db,models
from info.models import User

#创建ａｐｐ
app = create_app('dev')
#脚本控制器对象
manager = Manager(app)
# 让迁移和app和数据库建立管理
Migrate(app,db)
# 将数据库迁移的脚本添加到manager中
manager.add_command("mysql",MigrateCommand)

@manager.option('-u','-username',dest = 'username')
@manager.option('-p','-password',dest = 'password')
@manager.option('-m','-mobile',dest = 'mobile')
def createsuperuser(username,password,mobile):
    #创建超级管理员
    if not all([username,password,mobile]):
        print('缺少参数')
    else:
        user = User()
        user.nick_name = username
        user.password = password
        user.mobile = mobile
        #当is_admin为True 时，才是管理员用户
        user.is_admin = True
        try:
            db.session.add(user)
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            print(e)


if __name__=='__main__':
    print(app.url_map)

    manager.run()